import QtQuick 2.15
import QtQuick.Controls 2.15

Item {
    id: root

    // 枚举：日期属性
    enum DateProperty {
        E_Property_None = 0,  // 普通
        E_Property_Day = 1,    // 日期控件
        E_Property_Month = 2, // 月份控件
        E_Property_Year = 3   // 年份控件
    }

    // 枚举：滚动类型
    enum ScrollType {
        E_VERTICAL = 1,    // 垂直显示
        E_HORIZONTAL = 2   // 水平显示
    }

    // 属性
    property int minValue: 0
    property int maxValue: 100
    property int currentValue: minValue
    property int step: 1
    property int dateProperty: RollingBox.E_Property_None
    property int scrollType: RollingBox.E_VERTICAL
    property bool mouseClickEnabled: false
    property int device: 5  // 显示项数

    // 信号
    signal currentValueChanged(int value, int dateProperty)

    // 内部属性
    property int offset: 0
    property int mousePos: 0
    property bool isDragging: false

    // 计算属性
    readonly property int targetSize: scrollType === RollingBox.E_VERTICAL ? height : width
    readonly property int itemSize: targetSize / device
    readonly property int halfDevice: Math.floor(device / 2)

    // 背景选中框
    Rectangle {
        id: selectionRect
        color: Qt.rgba(234/255, 234/255, 234/255, 0.06)
        radius: 0
        
        x: scrollType === RollingBox.E_VERTICAL ? 0 : (width / device * (device / 2 + 1) - width / device / 2)
        y: scrollType === RollingBox.E_VERTICAL ? (height / device * 2) : 0
        width: scrollType === RollingBox.E_VERTICAL ? parent.width : (parent.width / device)
        height: scrollType === RollingBox.E_VERTICAL ? (parent.height / device) : parent.height
    }

    // 文本显示
    Repeater {
        model: halfDevice * 2 + 1
        
        Text {
            property int valueIndex: currentValue + (index - halfDevice) * step
            property int itemOffset: (index - halfDevice) * itemSize + offset
            
            text: getTextByProperty(valueIndex)
            font.pixelSize: getFontSize(itemOffset)
            font.family: "微软雅黑"
            color: getTextColor(itemOffset)
            
            x: scrollType === RollingBox.E_VERTICAL ? getTextX() : (width / 2 + itemOffset - width / 2)
            y: scrollType === RollingBox.E_VERTICAL ? (height / 2 + itemOffset - height / 2) : getTextY()
            
            width: scrollType === RollingBox.E_VERTICAL ? parent.width : implicitWidth
            height: scrollType === RollingBox.E_VERTICAL ? implicitHeight : parent.height
            
            horizontalAlignment: scrollType === RollingBox.E_VERTICAL ? getHorizontalAlignment() : Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
            
            visible: valueIndex >= minValue && valueIndex <= maxValue
        }
    }

    // 鼠标滚轮事件
    MouseArea {
        anchors.fill: parent
        acceptedButtons: Qt.LeftButton | Qt.MiddleButton
        
        onWheel: {
            if (minValue === maxValue) return
            
            var degrees = wheel.angleDelta.y / 8
            var steps = degrees / 15
            var targetOffset = itemSize * steps
            
            if (targetOffset !== 0) {
                offset = targetOffset
                updateOffsetValue()
                root.currentValueChanged(currentValue, dateProperty)
            }
        }
        
        onPressed: {
            if (mouseClickEnabled) {
                isDragging = true
                mousePos = scrollType === RollingBox.E_VERTICAL ? mouseY : mouseX
                updateOffsetValue()
            }
        }
        
        onPositionChanged: {
            if (mouseClickEnabled && isDragging) {
                var mouseCurrentPos = scrollType === RollingBox.E_VERTICAL ? mouseY : mouseX
                var mouseOffset = mouseCurrentPos - mousePos
                
                // 判断边界
                if ((currentValue === minValue && mouseOffset >= 0) ||
                    (currentValue === maxValue && mouseOffset <= 0)) {
                    return
                }
                
                // 限制偏移范围
                if (mouseOffset > itemSize) {
                    mouseOffset = itemSize
                } else if (mouseOffset < -itemSize) {
                    mouseOffset = -itemSize
                }
                
                offset = mouseOffset
                updateOffsetValue()
            }
        }
        
        onReleased: {
            if (mouseClickEnabled && isDragging) {
                isDragging = false
                snapToItem()
            }
        }
    }

    // 更新偏移值
    function updateOffsetValue() {
        if (offset >= itemSize && currentValue > minValue) {
            offset -= itemSize
            currentValue = Math.max(minValue, currentValue - step)
        } else if (offset <= -itemSize && currentValue < maxValue) {
            offset += itemSize
            currentValue = Math.min(maxValue, currentValue + step)
        }
        
        if (currentValue === maxValue || currentValue === minValue) {
            offset = 0
        }
    }

    // 吸附到项目
    function snapToItem() {
        var judge = offset < 0 ? -(itemSize / 2) : (itemSize / 2)
        
        if (offset < 0) {
            if (offset < judge) {
                offset = 0
            } else {
                offset = -itemSize
            }
        } else {
            if (offset < judge) {
                offset = 0
            } else {
                offset = itemSize
            }
        }
        
        updateOffsetValue()
    }

    // 获取文本
    function getTextByProperty(value) {
        var text = value.toString()
        
        switch (dateProperty) {
        case RollingBox.E_Property_Day:
            if (value < 10) {
                text = "0" + text
            }
            return text + "日"
        case RollingBox.E_Property_Month:
            return text + "月"
        case RollingBox.E_Property_Year:
            return text + "年"
        default:
            return text
        }
    }

    // 获取字体大小
    function getFontSize(itemOffset) {
        var baseSize = 12
        var offsetAbs = Math.abs(itemOffset)
        var fontSize = baseSize - Math.floor(offsetAbs / itemSize) * 2
        return Math.max(8, fontSize)
    }

    // 获取文本颜色
    function getTextColor(itemOffset) {
        var index = itemSize
        if (Math.abs(itemOffset) === index) {
            return Qt.rgba(1, 1, 1, 0.4)  // QColor(255, 255, 255, 102)
        } else if (Math.abs(itemOffset) === index * 2) {
            return Qt.rgba(1, 1, 1, 0.1)  // QColor(255, 255, 255, 25)
        }
        return Qt.rgba(1/255, 238/255, 195/255, 1)  // QColor(1, 238, 195)
    }

    // 获取文本X位置
    function getTextX() {
        switch (dateProperty) {
        case RollingBox.E_Property_Year:
            return 36
        case RollingBox.E_Property_Day:
            return -36
        default:
            return 0
        }
    }

    // 获取文本Y位置
    function getTextY() {
        return 0
    }

    // 获取水平对齐方式
    function getHorizontalAlignment() {
        switch (dateProperty) {
        case RollingBox.E_Property_Year:
            return Text.AlignLeft
        case RollingBox.E_Property_Day:
            return Text.AlignRight
        default:
            return Text.AlignHCenter
        }
    }

    // 值变化时重置偏移
    onCurrentValueChanged: {
        if (currentValue === maxValue || currentValue === minValue) {
            offset = 0
        }
    }
}

